﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
using TankGameV1._0.Properties;
using System.Threading;

namespace TankGameV1._0
{
    class EnemyTank : TankObject
    {
        public EnemyTank(int x, int y, int type, Direction dir)
            : base(x, y, img1, SetSpeed(type), SetLife(type), dir)
        {
            this.EnemyTankType = type;
            Born();
        }
        private static Image[] img1 =
        {
            Resources.enemy1U,
            Resources.enemy1D,
            Resources.enemy1L,
            Resources.enemy1R
        };
        private static Image[] img2 =
        {
            Resources.enemy2U,
            Resources.enemy2D,
            Resources.enemy2L,
            Resources.enemy2R
        };
        private static Image[] img3 = {
            Resources.enemy3U,
            Resources.enemy3D,
            Resources.enemy3L,
            Resources.enemy3R                                      
        };
        //存储敌人坦克的速度
        private static int _speed;
        //存储敌人坦克的生命
        private static int _life;

        public int EnemyTankType
        {
            get;
            set;
        }
        //通过一个静态方法来设置速度
        public static int SetSpeed(int type)
        {
            switch (type)
            {
                case 0:
                    _speed = 5;
                    break;
                case 1:
                    _speed = 6;
                    break;
                case 2:
                    _speed = 7;
                    break;
            }
            return _speed;
        }
        //通过一个静态方法来设置生命
        public static int SetLife(int type)
        {
            switch (type)
            {
                case 0:
                    _life = 1;
                    break;
                case 1:
                    _life = 2;
                    break;
                case 2:
                    _life = 3;
                    break;
            }
            return _life;
        }
        public bool IsStop = true;
        public int StopTime = 0;
        public override void Draw(Graphics g)
        {
            BornTime++;
            if (BornTime % 15 == 0)
            {
                IsMove = true;
            }
            if (IsMove)
            {
                if (IsStop) {
                    Move();
                }
                else
                {
                    StopTime++;
                    if (StopTime % 100 == 0)
                    {
                        IsStop = true;
                    }
                }
                switch (EnemyTankType)
                {
                    case 0:
                        switch (this.Dir)
                        {
                            case Direction.Up:
                                g.DrawImage(img1[0], this.X, this.Y);
                                break;
                            case Direction.Down:
                                g.DrawImage(img1[1], this.X, this.Y);
                                break;
                            case Direction.Left:
                                g.DrawImage(img1[2], this.X, this.Y);
                                break;
                            case Direction.Right:
                                g.DrawImage(img1[3], this.X, this.Y);
                                break;
                        }
                        break;
                    case 1:
                        switch (this.Dir)
                        {
                            case Direction.Up:
                                g.DrawImage(img2[0], this.X, this.Y);
                                break;
                            case Direction.Down:
                                g.DrawImage(img2[1], this.X, this.Y);
                                break;
                            case Direction.Left:
                                g.DrawImage(img2[2], this.X, this.Y);
                                break;
                            case Direction.Right:
                                g.DrawImage(img2[3], this.X, this.Y);
                                break;
                        }
                        break;
                    case 2:
                        switch (this.Dir)
                        {
                            case Direction.Up:
                                g.DrawImage(img3[0], this.X, this.Y);
                                break;
                            case Direction.Down:
                                g.DrawImage(img3[1], this.X, this.Y);
                                break;
                            case Direction.Left:
                                g.DrawImage(img3[2], this.X, this.Y);
                                break;
                            case Direction.Right:
                                g.DrawImage(img3[3], this.X, this.Y);
                                break;
                        }
                        break;
                }
            }
        }
        static Random r = new Random();
        //要让坦克自己动就要每次移动时产生随机数来改变坦克方向
        public override void Move()
        {
            if (r.Next(0, 100) < 5)
            {
                switch (r.Next(0, 4))
                {
                    case 0:
                        this.Dir = Direction.Up;
                        break;
                    case 1:
                        this.Dir = Direction.Down;
                        break;
                    case 2:
                        this.Dir = Direction.Left;
                        break;
                    case 3:
                        this.Dir = Direction.Right;
                        break;

                }
            }
            base.Move();//调用父类的Move方法
            if (r.Next(0, 100) < 5)
            {
                Fire();
            }
        }
        //开火方法
        public override void Fire()
        {
            SingleObject.GetObject().AddGameObject(new EnemyBullet(this,8,10,2));
        }
        //判断是否死亡方法
        public override void IsOver()
        {
            if(this.Life <= 0) {
                SingleObject.GetObject().AddGameObject(new Boom(this.X - 25, this.Y - 25));
                //如果被击中就销毁该坦克对象
                SingleObject.GetObject().RemoveObject(this);
                //播放坦克爆炸声音
                SoundPlayer sp = new SoundPlayer(Resources.fire);
                sp.Play();
                //敌人死亡有一定几率产生新坦克
                if (r.Next(0, 100) > 30)
                {
                    SingleObject.GetObject().AddGameObject(new EnemyTank(r.Next(0, 888),
                        r.Next(0, 666), r.Next(0, 3), Direction.Up));
                }
                //敌人死亡有一定几率产生装备
                if (r.Next(0, 100) > 40)
                {
                    SingleObject.GetObject().AddGameObject(new Equipment(this.X, this.Y, r.Next(0, 3)));
                }
            }
            else//敌人被击中但没有死亡
            {
                SoundPlayer sp = new SoundPlayer(Resources.hit);
                sp.Play();
            }           
        }
        //坦克出生方法,需要在坦克的构造函数中调用
        public override void Born()
        {
            SingleObject.GetObject().AddGameObject(new Born(this.X, this.Y));
        }
    }
}
